<?php
/**
 * CreateTime: 2023/12/27 14:16
 * User:LinQ
 */

namespace App\Http\Service\Client;

use App\Facade\UserFuncFacade;
use App\Func\File\FileFunc;
use App\Func\System\OperateLogFunc;
use App\Http\Service\BasicInterfaceService;
use App\Models\Client\ClientModel;
use App\State\StatusState;

class ClientService implements BasicInterfaceService
{

    public function getList(array $prams)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $clientModel = new ClientModel();
        $this->listSelectDefaultFilters($clientModel);
        if($filters){
            $this->listSelectFilters($clientModel,$filters);
        }
        $res = $clientModel->cListHasCom($clientModel,$page_size,$desc,[
            'client_id',
            'client_name',
            'client_code',
            'client_union_id',
            'owner_id as owner_str'
        ]);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.client')
        ]));
    }

    public function listSelectDefaultFilters($query){
        $query->pushFilter('status',1);
    }


    public function dispose($data): mixed
    {
        return $data;
    }

    public function listSelectFilters($query, $filters)
    {
        if(map_has_value($filters,'client_name')){
            $query->pushFilter('client_name',$filters['client_name'],'like');
        }
        if(map_has_value($filters,'client_code')){
            $query->pushFilter('client_code',$filters['client_code'],'like');
        }
    }

    public function getSelect()
    {
        $clientModel = new ClientModel();
        $res = $clientModel->scopeAddComIdWhere()->select([
            'client_id as key',
            'client_name as value'
        ])->where('status',1)->get();
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.client')
        ]));
    }

    public function getSelectHasPage($prams)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $clientModel = new ClientModel();
        $this->listSelectDefaultFilters($clientModel,$filters);
        if($filters){
            $this->listSelectFilters($clientModel,$filters);
        }
        $res = $clientModel->cListHasCom($clientModel,$page_size,$desc,[
            'client_name as value'
        ]);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.client')
        ]));
    }

    public function getInfo($id)
    {
        $clientModel = new ClientModel();

        $res = $clientModel->select([
            "*",
            "owner_id as owner_str",
            "com_id as com_str"
        ])->scopeAddComIdWhere()->where('client_id',$id)->first();
        // 处理文件
        $fileFunc = new FileFunc();
        $res['client_temp_law_files'] = $fileFunc->getFileInfoByFileIdToWeb($res['client_temp_law_file_id']);
        $res['client_temp_law_not_sign_files'] = $fileFunc->getFileInfoByFileIdToWeb($res['client_temp_law_not_sign_file_id']);

        s_c_return($res,trans('message.get_detail_ok',[
            'name' => trans('key.client')
        ]));
    }

    public function create($data)
    {
        $clientModel = new ClientModel();
        $userInfo =  UserFuncFacade::getLoginInfo();
        // 设置默认数据
        $insertData = [
            'client_name' => $data['client_name'],
            'client_code' => $data['client_code'],
            'owner_id' => $userInfo['user_id'],
            'com_id' => $userInfo['com_id'],
            'created_at' => date("Y-m-d h:i:s"),
            'updated_at' => date("Y-m-d h:i:s"),
        ];
        // 校验clientName 保证当前公司名称唯一
        $isExists = $clientModel->where(
            'client_name',$data['client_name']
        )->where(
            'com_id',$userInfo['com_id']
        )->exists();
        if($isExists){
            s_fail_return(null,trans('message.is_exists',[
                'name' => trans('key.client_name')
            ]));
        }


        // 设置默认值
        $insertData['client_union_id'] = (\Symfony\Component\Uid\UuidV4::v6()->toRfc4122());
        $res = $clientModel->insertGetId($insertData);

        // 添加操作记录
        OperateLogFunc::addOperate(
            'ClientService',$res,'create');

        s_c_return(['client_id'=>$res],trans('message.created_ok',[
            'name' => trans('key.client')
        ]));
    }

    public function fakeDelete($id)
    {
        $clientModel = new ClientModel();
        // 通过状态机修改状态
        $res = (new StatusState())->stateChange($id,
            StatusState::OK_STATUS,
            StatusState::DELETE_STATUS,
            $clientModel,
            'client_id'
        );
        if(!$res){
            s_fail_return(null,trans('message.fake_del_ok',[
                'name' => trans('key.client')
            ]));
        }
        // 添加操作记录
        OperateLogFunc::addOperate(
            'ClientService',$res,'fakeDelete'
        );
        s_c_return(null,trans('message.fake_del_ok',[
            'name' => trans('key.client')
        ]));
    }


    public function bindLawCaseTemp($data){
        try {
            $clientModel = new ClientModel();
            $fmtData = [];
            if (isset($data['client_temp_law_file_id'])) {
                $fmtData['client_temp_law_file_id'] = $data['client_temp_law_file_id'];
            }
            if (isset($data['client_temp_law_not_sign_file_id'])) {
                $fmtData['client_temp_law_not_sign_file_id'] = $data['client_temp_law_not_sign_file_id'];
            }
            $res = $clientModel->where('client_id',$data['client_id'])->update($fmtData);
            if($res){
                // 添加操作记录
                OperateLogFunc::addOperate(
                    'ClientService',$res,'update'
                );
                s_c_return(null,trans('message.update_ok',[
                    'name' => trans('key.client')
                ]));
            }
            s_c_return(null,trans('message.update_err',[
                'name' => trans('key.client')
            ]));
        }catch (\Exception $exception){
            s_c_return(null,trans('message.update_err',[
                'name' => trans('key.client')
            ]));
        }
    }

    public function delete($id)
    {

    }

    public function update($data)
    {
        $clientModel = new ClientModel();
        $clientInfo = $clientModel->where('client_id',$data['client_id'])->first();
        // 校验clientName 保证当前公司名称唯一
        $isExists = $clientModel->where(
            'client_name',$data['client_name']
        )->where(
            'com_id',$clientInfo['com_id']
        )->where('client_id','!=',$data['client_id'])
        ->exists();
        $fmtData = [
            'client_name' => $data['client_name']
        ];
        if($isExists){
            s_fail_return(null,trans('message.is_exists',[
                'name' => trans('key.client_name')
            ]));
        }
        try {
            $res = $clientModel->where('client_id',$data['client_id'])->update($fmtData);
            if($res){
                // 添加操作记录
                OperateLogFunc::addOperate(
                    'ClientService',$res,'update'
                );
                s_c_return(null,trans('message.update_ok',[
                    'name' => trans('key.client')
                ]));
            }
            s_c_return(null,trans('message.update_err',[
                'name' => trans('key.client')
            ]));
        }catch (\Exception $exception){
            s_c_return(null,trans('message.update_err',[
                'name' => trans('key.client')
            ]));
        }
    }

    public function fakeDeleteByIds($ids)
    {

    }
}
